
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

namespace Swaf.Gui
{
	public delegate void ActionTrigger();
	public delegate bool BoolActionTrigger();
	public delegate void ActionParamTrigger(object param);

	/// <summary>
	/// Summary description for GenericAction.
	/// </summary>
	public class GenericAction : IAction
	{
		protected string m_id, m_name="", m_mediumName="", m_description="";
		protected IBusinessProcess m_parentProcess;
		protected bool m_enabled;
		//This replaces the m_enabled for more options. m_enabled is left for backward compatibility.
		protected ActionAvailability m_availability = ActionAvailability.notSet;

		protected IList m_states = null;
		protected string m_currentState = null;

		protected bool m_allowOpenParams = false;
		protected IList m_validParams = null;
        protected object m_currentParam = null;

		protected ActionTrigger m_basicTrigger = null;
		protected ActionParamTrigger m_paramTrigger = null;
		protected BoolActionTrigger m_boolTrigger = null;

		/// <summary>
		/// Initialzes the action with no state or params.  This is the most basic configuration this object
		/// can have.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="enabled"></param>
		/// <param name="basicTrigger"></param>
		public GenericAction(string id, bool enabled, ActionTrigger basicTrigger, IBusinessProcess parentProcess)
		{
			m_id = id;
			m_parentProcess = parentProcess;
			m_enabled = enabled;
			m_states = new ArrayList();
			m_validParams = new ArrayList();
			m_basicTrigger = basicTrigger;
		}
		public GenericAction(string id, bool enabled, bool dummyToIndicateBoolActionTrigger,
			BoolActionTrigger basicTrigger, IBusinessProcess parentProcess)
		{
			m_id = id;
			m_parentProcess = parentProcess;
			m_enabled = enabled;
			m_states = new ArrayList();
			m_validParams = new ArrayList();
			m_boolTrigger = basicTrigger;
		}

		/// <summary>
		/// Initializes the action to support states but not params.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="enabled"></param>
		/// <param name="currentState"></param>
		/// <param name="operationStates"></param>
		public GenericAction(string id, bool enabled, string currentState, ActionTrigger basicTrigger, params string[] operationStates)
		{
			m_id = id;
			m_enabled = enabled;
			m_currentState = currentState;
			m_states = operationStates;
			m_validParams = new ArrayList();
			m_basicTrigger = basicTrigger;
		}

		/// <summary>
		/// Initialzes the action to support trigger parameters but not states.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="enabled"></param>
		/// <param name="allowOpenParams"></param>
		/// <param name="validParams"></param>
		public GenericAction(string id, bool enabled, bool allowOpenParams, ActionParamTrigger paramTrigger, params object[] validParams)
		{
			m_id = id;
			m_enabled = enabled;
			m_allowOpenParams = allowOpenParams;
			m_validParams = validParams;
			m_states = new ArrayList();
			m_paramTrigger = paramTrigger;
		}

        /// <summary>
        /// Initialzes the action to support trigger parameters but not states.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="enabled"></param>
        /// <param name="allowOpenParams"></param>
        /// <param name="validParams"></param>
        public GenericAction(string id, bool enabled, bool allowOpenParams, IBusinessProcess parentProcess, ActionParamTrigger paramTrigger, params object[] validParams)
        {//onyx 09/28/07 added new constructor signature
            m_id = id;
            m_enabled = enabled;
            m_allowOpenParams = allowOpenParams;
            m_validParams = validParams;
            m_parentProcess = parentProcess;
            m_states = new ArrayList();
            m_paramTrigger = paramTrigger;
        }

		public System.Collections.IList States
		{
			get
			{
				//TODO: Return a read-only list
				return m_states;
			}
			set
			{
				m_states = value;
				if (ActionChanged != null)
					ActionChanged(this, ActionChangeType.OperationState);
			}
		}

		public bool trigger(object parameter)
		{
			bool rc = true;
			if (m_paramTrigger != null)
				m_paramTrigger(parameter);
			else if (m_basicTrigger != null)
				m_basicTrigger();
			else if (m_boolTrigger != null)
				rc = m_boolTrigger();
			else
			{
				//TODO Log the fact that a call was made to this method without a degate being configured.
			}
			return rc;
		}

		public bool trigger()
		{
			bool c = true;
			if (m_basicTrigger != null)
				m_basicTrigger();
			else if (m_boolTrigger != null)
				c = m_boolTrigger();
			else
			{
				//TODO Log the fact that a call was made to this method without a degate being configured.
			}
			return c;
		}

		public event Swaf.Gui.ActionChanged ActionChanged;

		public bool AllowsOpenParameters
		{
			get
			{
				return m_allowOpenParams;
			}
			set
			{
				if (ActionChanged != null)
					ActionChanged(this, ActionChangeType.ParameterOptions);
				m_allowOpenParams = value;
			}
		}

		public string Id
		{
			get
			{
				return m_id;
			}
		}

		public virtual string ShortName
		{
			get
			{
				return m_name;
			}
			set
			{
				m_name = value;
			}
		}

		public virtual string Description
		{
			get
			{
				return m_description;
			}
			set
			{
				m_description = value;
			}
		}

		public virtual string MediumName
		{
			get
			{
				return m_mediumName;
			}
			set
			{
				m_mediumName = value;
			}
		}

		public bool Enabled
		{
			get
			{
				return m_enabled;
			}
			set
			{
				m_enabled = value;
				if(ActionChanged != null)
					ActionChanged(this, ActionChangeType.Enabled);
			}
		}

		public ActionAvailability Availability
		{
			get
			{
				if(m_availability == ActionAvailability.notSet)
					return m_enabled?ActionAvailability.enabled:ActionAvailability.disabled;
				return m_availability;
			}
			set
			{
				m_availability = value;
				m_enabled = m_availability == ActionAvailability.enabled;
				if (ActionChanged != null)
					ActionChanged(this, ActionChangeType.Availability);
			}
		}

		public System.Collections.IList ValidParameterOptions
		{
			get
			{
				//TODO: Return a read-only list.
				return m_validParams;
			}
			set
			{
				m_validParams = value;
				if (ActionChanged != null)
					ActionChanged(this, ActionChangeType.ParameterOptions);
			}
		}

		public string CurrentOperationState
		{
			get
			{
				return m_currentState;
			}
			set
			{
				m_currentState = value;
				if (ActionChanged != null)
					ActionChanged(this, ActionChangeType.OperationState);
			}
		}

        public object CurrentParameterOption 
        {
            get 
            { return m_currentParam; } 
            set 
            { 
                m_currentParam = value;
                if (ActionChanged != null)
                    ActionChanged(this, ActionChangeType.ParameterOptions);
            } 
        }

		public IBusinessProcess ParentProcess
		{
			get { return m_parentProcess; }
		}
	}
}
